package chixing.day15.work;

import java.util.HashMap;
import java.util.Map;

public class Count {

    public String mostFrequentWord(String sentence) {
        String[] words = sentence.split(" ");
        Map<String, Integer> wordCount = new HashMap<>();
        for (String word : words) {
            if (wordCount.containsKey(word)) {
                wordCount.put(word, wordCount.get(word) + 1);
            } else {
                wordCount.put(word, 1);
            }
        }
        System.out.println("单词出现的次数统计:");
        for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
        String mostFrequentWord = "";
        int maxCount = 0;
        for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
            if (entry.getValue() > maxCount) {
                mostFrequentWord = entry.getKey();
                maxCount = entry.getValue();
            }
        }
        return mostFrequentWord;

    }
    public static void main(String[] args) {
        Count count = new Count();
        String sentence = "the quick brown fox jumps over the lazy dog the quick brown fox";

        System.out.println(count.mostFrequentWord(sentence));
    }
}
